package com.atguigu.gmall.product.controller;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.gmall.common.cache.GmallCache;
import com.atguigu.gmall.common.constant.RedisConst;
import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.model.product.*;
import com.atguigu.gmall.product.service.ManageService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * 分类与属性管理控制层
 */
@Api(value = "ManageController", tags = "分类与属性管理控制层" )
@RestController
@RequestMapping(value = "/admin/product")
public class ManageController {
    @Autowired
    private ManageService manageService;

    /**
     * 查询所有一级分类列表
     * @return
     */
    @ApiOperation("查询所有一级分类列表")
    @GetMapping(value = "/getCategory1")
    public Result<List<BaseCategory1>> getCategory1List(){
        List<BaseCategory1> category1List = manageService.getCategory1List();
        return Result.ok(category1List);
    }
    /**
     * 根据一级分类查询二级分类列表
     * @param category1Id
     * @return
     */
    @ApiOperation("根据一级分类查询二级分类列表")
    @GetMapping(value = "/getCategory2/{category1Id}")
    public Result<List<BaseCategory2>> getCategory2List(@PathVariable(value = "category1Id") Long category1Id){
        List<BaseCategory2> category2List = manageService.getCategory2List(category1Id);
        return Result.ok(category2List);
    }
    /**
     * 根据二级分类查询三级分类列表
     * @param category2Id
     * @return
     */
    @ApiOperation("根据二级分类查询三级分类列表")
    @GetMapping(value = "/getCategory3/{category2Id}")
    public Result<List<BaseCategory3>> getCategory3List(@PathVariable(value = "category2Id") Long category2Id){
        List<BaseCategory3> category3List = manageService.getCategory3List(category2Id);
        return Result.ok(category3List);
    }
    /**
     * 根据分类查询所有规格信息列表:查询1 2 3级分类对应的规格信息列表,若只查询某一个分类下的规格列表则其他分类的值传0
     * @param category1Id
     * @param category2Id
     * @param category3Id
     * @return
     */
    @ApiOperation("根据分类查询所有规格信息列表")
    @GetMapping(value = "/attrInfoList/{category1Id}/{category2Id}/{category3Id}")
    public Result<List<BaseAttrInfo>> getBaseAttrInfoList(@PathVariable(value = "category1Id") Long category1Id,
                                                          @PathVariable(value = "category2Id") Long category2Id,
                                                          @PathVariable(value = "category3Id") Long category3Id){
        List<BaseAttrInfo> baseAttrInfoList = manageService.getBaseAttrInfoList(category1Id, category2Id, category3Id);
        return Result.ok(baseAttrInfoList);
    }

    /**
     * 保存baseAttrInfo
     * @param baseAttrInfo
     * @return
     */
    @PostMapping("/saveAttrInfo")
    public Result saveAttrInfo(@RequestBody BaseAttrInfo baseAttrInfo){
        baseAttrInfo = manageService.saveAttrInfo(baseAttrInfo);
        return Result.ok(baseAttrInfo);
    }
    /**
     * 根据平台属性id查询平台属性值列表
     * @param id
     * @return
     */
    @GetMapping(value = "/getAttrValueList/{id}")
    public Result<List<BaseAttrValue>> getAttrValueList(@PathVariable(value = "id") Long id) {
        List<BaseAttrValue> valueById = manageService.getValueById(id);
        return Result.ok(valueById);
    }

    /**
     * 分页查询SpuInfo的信息
     * @param category3Id
     * @param page
     * @param size
     * @return
     */
    @GetMapping(value = "/{page}/{size}")
    public Result<IPage<SpuInfo>> getSpuInfoList(Long category3Id,
                                                 @PathVariable(value = "page") Integer page,
                                                 @PathVariable(value = "size") Integer size){
        IPage<SpuInfo> spuInfoList = manageService.getSpuInfoList(category3Id,page,size);
        return Result.ok(spuInfoList);
    }

    /**
     * 分页查询品牌的信息
     * @param page
     * @param size
     * @return
     */
    @GetMapping(value = "/baseTrademark/{page}/{size}")
    public Result<IPage<BaseTrademark>> baseTrademark(@PathVariable(value = "page") Integer page,
                                                      @PathVariable(value = "size") Integer size){
        IPage<BaseTrademark> baseTrademarkIPage = manageService.getTrademarkList(page,size);
        return Result.ok(baseTrademarkIPage);
    }
    /**
     * 分页查询skuinfo
     * @param page
     * @param size
     * @return
     */
    @GetMapping(value = "/list/{page}/{size}")
    public Result<IPage<SkuInfo>> list(@PathVariable(value = "page") Integer page,
                                       @PathVariable(value = "size") Integer size){
        IPage<SkuInfo> skuInfoList = manageService.getSkuInfoList(page, size);
        return Result.ok(skuInfoList);
    }
    /**
     * 查询所有的销售属性
     * @return
     */
    @GetMapping(value = "/baseSaleAttrList")
    public Result<List<BaseSaleAttr>> baseSaleAttrList(){
        List<BaseSaleAttr> baseSaleAttr =manageService.getBaseSaleAttr();
        return Result.ok(baseSaleAttr);
    }
    /**
     * 查询所有的品牌列表
     * @return
     */
    @GetMapping(value = "/baseTrademark/getTrademarkList")
    public Result<List<BaseTrademark>> baseTrademark(){
        List<BaseTrademark> trademark = manageService.getTrademark();
        return Result.ok(trademark);
    }


    /**
     * 新增品牌
     * @param baseTrademark
     * @return
     */
    @PostMapping(value = "/baseTrademark/save")
    public Result<BaseTrademark> addTrademark(@RequestBody BaseTrademark baseTrademark){
        manageService.addTrademark(baseTrademark);
        return Result.ok();
    }

    /**
     * 修改品牌
     * @param baseTrademark
     * @return
     */
    @PutMapping(value = "/baseTrademark/update")
    public Result<BaseTrademark> updateTrademark(@RequestBody BaseTrademark baseTrademark){
        manageService.updateTrademark(baseTrademark);
        return Result.ok();
    }

    /**
     * 根据Id查询品牌
     * @param id
     * @return
     */
    @GetMapping(value = "/baseTrademark/get/{id}")
    public Result<BaseTrademark> getTrademark(@PathVariable(value = "id")Long id){
        BaseTrademark baseTrademark = manageService.getTrademarkById(id);
        return Result.ok(baseTrademark);
    }

    /**
     * 根据Id删除品牌
     * @param id
     * @return
     */
    @DeleteMapping(value = "/baseTrademark/remove/{id}")
    public Result<BaseTrademark> deleteTrademark(@PathVariable(value = "id")Long id){
        manageService.deleteTrademarkById(id);
        return Result.ok();
    }
    /**
     * 新增spu
     * @param spuInfo
     * @return
     */
    @PostMapping(value = "/saveSpuInfo")
    public Result saveSpuInfo(@RequestBody SpuInfo spuInfo){
        spuInfo = manageService.saveSpuInfo(spuInfo);
        return Result.ok(spuInfo);
    }

    /**
     * 查询所有的spu图片列表
     * @param id
     * @return
     */
    @GetMapping(value = "/spuImageList/{id}")
    public Result<List<SpuImage>> spuImageList(@PathVariable(value = "id")Long id){
        List<SpuImage> spuImages = manageService.getSpuImageList(id);
        return Result.ok(spuImages);
    }

    /**
     * 根据spuId查询销售属性列表
     * @param id
     * @return
     */
    @GetMapping(value = "/spuSaleAttrList/{id}")
    public Result<List<SpuSaleAttr>> spuSaleAttrList(@PathVariable(value = "id") Long id){
        List<SpuSaleAttr> spuSaleAttrList = manageService.getSpuSaleAttrList(id);
        return Result.ok(spuSaleAttrList);
    }

    /**
     * 保存sku信息
     * @param skuInfo
     * @return
     */
    @PostMapping(value = "/saveSkuInfo")
    public Result saveSkuInfo(@RequestBody SkuInfo skuInfo){
        skuInfo = manageService.saveSkuInfo(skuInfo);
        return Result.ok(skuInfo);
    }

    /**
     * 根据skuId查询sku信息
     * @param skuId
     * @return
     */
    @GmallCache(prefix = RedisConst.SKUKEY_PREFIX)
    @GetMapping("/getSkuInfo/{skuId}")
    public SkuInfo getSkuInfo(@PathVariable("skuId") Long skuId){
        //redis+lua获取
//        SkuInfo skuInfo = manageService.getSkuByRedis(skuId);
        //redission
//        SkuInfo skuInfo = manageService.getSkuByRedisson(skuId);
        SkuInfo skuInfo = manageService.getSkuInfo(skuId);
        return skuInfo;
    }
    /**
     * 通过三级分类id查询分类信息
     * @param category3Id
     * @return
     */
    @GmallCache(prefix = "categoryViewByCategory3Id:")
    @GetMapping("/getCategoryView/{category3Id}")
    public BaseCategoryView getCategoryView(@PathVariable("category3Id")Long category3Id){
        return manageService.getCategoryViewByCategory3Id(category3Id);
    }

    /**
     * 获取sku最新价格
     * @param skuId
     * @return
     */
    @GmallCache(prefix = "skuPrice:")
    @GetMapping("/getSkuPrice/{skuId}")
    public BigDecimal getSkuPrice(@PathVariable Long skuId){
        return manageService.getSkuPrice(skuId);
    }

    /**
     * 根据spuId，skuId 查询销售属性集合
     * @param skuId
     * @param spuId
     * @return
     */
    @GmallCache(prefix = "spuSaleAttrListCheckBySku:")
    @GetMapping("/getSpuSaleAttrListCheckBySku/{skuId}/{spuId}")
    public List<SpuSaleAttr> getSpuSaleAttrListCheckBySku(@PathVariable("skuId") Long skuId,@PathVariable("spuId") Long spuId){
        return manageService.getSpuSaleAttrListCheckBySku(skuId,spuId);
    }
    /**
     * 根据spuId 查询map 集合属性
     * @param spuId
     * @return
     */
    @GmallCache(prefix = "saleAttrValuesBySpu:")
    @GetMapping("/getSkuValueIdsMap/{spuId}")
    public Map getSkuValueIdsMap(@PathVariable("spuId") Long spuId){
        return manageService.getSkuValueIdsMap(spuId);
    }

    /**
     * 获取全部分类信息
     * @return
     */
    @GmallCache(prefix = "getBaseCategoryList:")
    @GetMapping("/getBaseCategoryList")
    public List<JSONObject> getBaseCategoryList(){
        return manageService.getBaseCategoryList();
    }

    /**
     * 查询品牌信息详情
     * @param id
     * @return
     */
    @GmallCache(prefix = "baseTrademark")
    @GetMapping(value = "/getById/{id}")
    public BaseTrademark getById(@PathVariable(value = "id") Long id){
        return manageService.getById(id);
    }

    /**根据sku的id查询平台属性信息
     *
     * @param skuId
     */
    @GmallCache(prefix = "BaseAttrInfoBySku")
    @GetMapping(value = "/selectBaseAttrInfoBySku/{skuId}")
    public List<BaseAttrInfo> selectBaseAttrInfoBySku(@PathVariable(value = "skuId") Long skuId){
        return manageService.selectBaseAttrInfoBySku(skuId);
    }

    /**
     * 修改sku库存
     * @param id
     * @param num
     * @return
     */
    @PutMapping("/updateSkuStock/{id}/{num}")
    public int updateSkuStock(@PathVariable(value = "id") Long id, @PathVariable(value = "num")Integer num){
       return manageService.updateSkuStock(id, num);
    }

    /**
     * 取消订单回滚库存
     * @param id
     * @param num
     * @return
     */
    @PutMapping("/updateCancelSkuStock/{id}/{num}")
    public void updateCancelSkuStock(@PathVariable(value = "id") Long id, @PathVariable(value = "num")Integer num){
        manageService.updateCancelSkuStock(id,num);
    }



    /**
     * 上架
     * @param skuId
     * @return
     */
    @GetMapping(value = "/onSale/{skuId}")
    public Result<SkuInfo> onSale(@PathVariable(value = "skuId")Long skuId){
        SkuInfo skuInfo =manageService.UpOrDown(skuId,1);
        return Result.ok(skuInfo);
    }

    /**
     * 下架
     * @param skuId
     * @return
     */
    @GetMapping(value = "/cancelSale/{skuId}")
    public Result<SkuInfo> cancelSale(@PathVariable(value = "skuId")Long skuId){
        SkuInfo skuInfo =manageService.UpOrDown(skuId,0);
        return Result.ok(skuInfo);
    }
}
